#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;
using db = double;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 2e5 + 10;

int a[N],n,m;

void solve() {
	n = in,m = in; rep(i, 1, n) a[i] = (in + a[i - 1]) % m;
	int ans = 1e9;
	rep(i, 1, n) {
		int j = n - i,t = __builtin_popcount(j);
		if(a[i] == 0) { ans = min(ans,t); continue; }
		int now = j;
		while(now) {
			if(a[now] == a[i] && now + j < n) {
				ans = min(ans,t); break;
			} now = (now - 1) & j;
		}
	}
	if(ans > 100) puts("-1");
	else printf("%d\n",ans);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
	for(int T = in; T; T--) solve();
}
